﻿using System; 
using System.Collections.Generic;
using System.Data;

using SiteUnity.Frame.Common.Tools.Class;
using SiteUnity.Layers.Entity.General;
using SiteUnity.Layers.Entity.PageItems.Contents;


namespace SiteUnity.Layers.Core.Provider.PageItems
{
    public class ContentProvider: Provider
    {
        public ContentProvider()
        {
            SetDefaultConnection();
        }
        public List<ContentType> selectContentType()
        {
            Database.clear();
            DataTable datatable = Database.executeQuery("SU_selectContentType");
            Database.close();

            return ClassMapper.ToClassList<ContentType>(datatable);
        }

        public List<Content> selectContentByPage(Page page)
        {
            Database.clear();
            Database.addParameter("@page_guid", page.Guid);

            DataTable datatable = Database.executeQuery("SU_selectContentByPage");
            Database.close();

            return ClassMapper.ToClassList<Content>(datatable);
        }

        public Content insertContent(Page page, Content Content)
        {
            Database.clear();
            Database.addParameter("@pageGuid", page.Guid);
            Database.addParameter("@ContentText", Content.ContentText);
            Database.addParameter("@ContentTitle", Content.ContentTitle);
            Database.addParameter("@TypeID", Content.TypeID);

            DataTable datatable = Database.executeQuery("SU_insertContent");
            Database.close();

            if (datatable.Rows.Count != 0)
                return ClassMapper.ToClass<Content>(datatable.Rows[0]);
            else
                return Content;
        }
    }
}
